package com.helloniu.wb {

public class UndoManager {
    public function UndoManager() {
        clear();
    }

    public static function clear():void {
        arr = new Array();
        idx = 0 ;
    }

    public static function addUndoable(obj:UndoableObject):void {
        arr.push(obj);

        idx = arr.length - 1;
    }

    public static function isUndoable():Boolean {
        var len:Number = arr.length;

        if(len <= 0 || idx <= 0) {
            return false;
        }

        if(idx < len) {
            return true;
        }

        return false;
    }

    public static function isRedoable():Boolean {
        var len:Number = arr.length;

        if(len <= 0 || idx < 0) {
            return false;
        }

        if(idx >= len - 1) {
            return false;
        }

        return true;
    }

    public static function undo():void {
        var unobj:UndoableObject = arr[idx] as UndoableObject;

        if(unobj != null) {
            unobj.undo();
            idx--;
        }
    }

    public static function redo():void {
        idx++;
        var unobj:UndoableObject = arr[idx] as UndoableObject;

        if(unobj != null) {
            unobj.redo();
        }
    }

    private static var arr:Array = new Array();
    private static var idx:Number = 0;
}
}